import { combineReducers } from 'redux'
import data from './data'
// 用户信息
const user = (state = data.userInfo, action) => {
    const name = 'user'
    switch (action.type) {
        case 'set': //缓存用户信息
            data.setStorage(name, action.data)
            return action.data
        case 'logout':
            data.clearStorage()
            return state
        default:
            return state
    }
}
// 全局权限菜单
const menus = (state = data.menus, action) => {
    switch (action.type) {
        case 'setMenus':
            sessionStorage.setItem('menus', JSON.stringify(action.data))
            return action.data
        default:
            return state || []
    }
}
const manualLoad = (state = data.manualLoad, action) => {
    switch (action.type) {
        case 'openManualLoad':
            return data.manualLoad = true
        case 'closeManualLoad':
            return data.manualLoad = false
        case 'stopManualLoad':
            return data.manualLoad = 'stop'
        case 'startManualLoad':
            return data.manualLoad = false
        default:
            return state
    }
}
const initState = false;
const loadStatus = (state = initState, action) => {
    // console.log(data.manualLoad)
    if (data.manualLoad) {
        if (data.manualLoad == 'stop') return false
        return true
    } else {
        switch (action.type) {
            case 'OPENPAGELOADING': {
                return true
            }
            case 'CLOSEPAGELOADING': {
                return false
            }
            default: {
                return state
            }
        }
    }
}

// 用户按钮权限
const ident = (state = data.ident, action) => {
    const name = 'ident'
    switch (action.type) {
        case 'setIdent': //缓存用户信息
            sessionStorage.setItem(name, action.data)
            return action.data
        default:
            return state
    }
}
//页面搜索，页码，条数
const searchValue = (state = data.searchValue, action) => {
    switch (action.type) {
        case 'ClearSearchValues': //清楚搜索信息
            sessionStorage.removeItem('searchValue')
            return ''
        case 'SaveSearchValues': //缓存搜索信息
            sessionStorage.setItem('searchValue', JSON.stringify(action.data))
            return action.data
        default:
            return state || { pageNo: 1, pageSize: 10 }
    }
}
//页面搜索，页码，条数
const pageTabValue = (state = data.pageTabValue, action) => {
    switch (action.type) {
        case 'ClearPageTabValue': //清楚搜索信息
            sessionStorage.removeItem('pageTabValue')
            return ''
        case 'SavePageTabValue': //缓存搜索信息
            sessionStorage.setItem('pageTabValue', JSON.stringify(action.data))
            return action.data
        default:
            return state
    }
}
// 页面table滚动距离
const tableScrollTop = (state = data.tableScrollTop, action) => {
    // console.log(data.manualLoad)
    if (data.tableScrollTop) {
        return data.tableScrollTop
    }
    else {
        switch (action.type) {
            case 'Set_tableScrollTop': {
                return action.data
            }
            case 'Clear_tableScrollTop': {
                return 0
            }
            default: {
                return state
            }
        }
    }
}
// 缓存页面数据
const dataSource = (state = data.dataSource, action) => {
    switch (action.type) {
        case 'ClearDataSource': //清楚搜索信息
            sessionStorage.removeItem('dataSource')
            return ''
        case 'SaveDataSource': //缓存搜索信息
            sessionStorage.setItem('dataSource', JSON.stringify(action.data))
            return action.data
        default:
            return state
    }
}
// 缓存页面数据
const pageActionTime = (state = data.pageActionTime, action) => {
    switch (action.type) {
        case 'ResetPageActionTime': //清楚搜索信息
            data.pageActionTime = 11130000 || 120 * 60 * 1000
            return data.pageActionTime
        case 'IntervalPageActionTime': //清楚搜索信息
            console.log(window.location.hash);
            if (window.location.hash === '#/login') return false
            // window.addEventListener('mousemove', () => {
            //     data.pageActionTime = process.env.NODE_ENV === 'development' ? 30000 : 120 * 60 * 1000
            // })
            const timer = setInterval(() => {
                console.log(data.pageActionTime);

                if (window.location.hash === '#/login') {
                    clearInterval(timer)
                    data.pageActionTime = 11130000 || 120 * 60 * 1000
                    return false
                }
                if (data.pageActionTime <= 0) {
                    // Reducers.dispatch({ type: 'ResetPageActionTime' })
                    data.pageActionTime = 11130000 || 120 * 60 * 1000
                    localStorage.removeItem('RSnetToken');
                    localStorage.removeItem('roleName');
                    clearInterval(timer)
                    sessionStorage.clear();
                    window.location.href = "";
                } else {
                    data.pageActionTime = data.pageActionTime - 1000
                    // Reducers.dispatch({ type: 'IntervalPageActionTime' })
                }
            }, 1000);
            return data.pageActionTime

        default:
            return state
    }
}

// 输出对应的state
const Reducers = combineReducers({
    user,
    menus,
    manualLoad,
    loadStatus,
    searchValue,
    ident,
    pageTabValue,
    tableScrollTop,
    dataSource,
    pageActionTime
})
export default Reducers
